<template>
  <div class="exam-home">
    <!-- 欢迎横幅 -->
    <div class="welcome-banner">
      <div class="banner-content">
        <div class="welcome-text">
          <h1>在线考试系统</h1>
          <p>智能化考试平台，公平公正的评估体系</p>
        </div>
        <div class="banner-stats">
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalExams }}</div>
            <div class="stat-label">考试科目</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalStudents }}</div>
            <div class="stat-label">参考人数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.passRate }}%</div>
            <div class="stat-label">通过率</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快速入口 -->
    <div class="quick-actions">
      <div class="action-grid">
        <div class="action-card" @click="goToExamList">
          <div class="action-icon">
            <el-icon><Document /></el-icon>
          </div>
          <div class="action-content">
            <h3>参加考试</h3>
            <p>查看可参加的考试列表</p>
          </div>
        </div>
        
        <div class="action-card" @click="goToMyExams">
          <div class="action-icon">
            <el-icon><User /></el-icon>
          </div>
          <div class="action-content">
            <h3>我的考试</h3>
            <p>查看考试记录和成绩</p>
          </div>
        </div>
        
        <div class="action-card" @click="goToPractice">
          <div class="action-icon">
            <el-icon><EditPen /></el-icon>
          </div>
          <div class="action-content">
            <h3>模拟练习</h3>
            <p>练习题库，提升能力</p>
          </div>
        </div>
        
        <div class="action-card" @click="goToCertificates">
          <div class="action-icon">
            <el-icon><Medal /></el-icon>
          </div>
          <div class="action-content">
            <h3>证书管理</h3>
            <p>查看获得的证书</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 推荐考试 -->
    <div class="featured-exams">
      <div class="section-header">
        <h2>推荐考试</h2>
        <el-button text @click="viewAllExams">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="exams-grid">
        <div 
          v-for="exam in featuredExams" 
          :key="exam.id"
          class="exam-card"
          @click="viewExam(exam)"
        >
          <div class="exam-header">
            <div class="exam-category">{{ exam.category }}</div>
            <div class="exam-difficulty">
              <el-tag :type="getDifficultyType(exam.difficulty)" size="small">
                {{ exam.difficulty }}
              </el-tag>
            </div>
          </div>
          
          <div class="exam-content">
            <h3 class="exam-title">{{ exam.title }}</h3>
            <p class="exam-desc">{{ exam.description }}</p>
            
            <div class="exam-meta">
              <div class="meta-item">
                <el-icon><Clock /></el-icon>
                <span>{{ exam.duration }}分钟</span>
              </div>
              <div class="meta-item">
                <el-icon><Document /></el-icon>
                <span>{{ exam.questionCount }}题</span>
              </div>
              <div class="meta-item">
                <el-icon><User /></el-icon>
                <span>{{ exam.participants }}人参加</span>
              </div>
            </div>
            
            <div class="exam-footer">
              <div class="exam-score">
                <span class="label">及格分：</span>
                <span class="value">{{ exam.passingScore }}分</span>
              </div>
              <el-button type="primary" @click.stop="startExam(exam)">
                开始考试
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 最近考试记录 -->
    <div class="recent-exams">
      <div class="section-header">
        <h2>最近考试</h2>
        <el-button text @click="viewAllRecords">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="records-list">
        <div 
          v-for="record in recentRecords" 
          :key="record.id"
          class="record-item"
        >
          <div class="record-info">
            <div class="exam-name">{{ record.examTitle }}</div>
            <div class="exam-time">{{ formatDate(record.completedAt) }}</div>
          </div>
          
          <div class="record-score">
            <div class="score" :class="getScoreClass(record.score)">
              {{ record.score }}分
            </div>
            <div class="status">
              <el-tag :type="record.passed ? 'success' : 'danger'" size="small">
                {{ record.passed ? '通过' : '未通过' }}
              </el-tag>
            </div>
          </div>
          
          <div class="record-actions">
            <el-button size="small" @click="viewResult(record)">
              查看详情
            </el-button>
            <el-button size="small" type="primary" @click="retakeExam(record)">
              重新考试
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 考试公告 -->
    <div class="exam-announcements">
      <div class="section-header">
        <h2>考试公告</h2>
        <el-button text @click="viewAllAnnouncements">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="announcements-list">
        <div 
          v-for="announcement in announcements" 
          :key="announcement.id"
          class="announcement-item"
          @click="viewAnnouncement(announcement)"
        >
          <div class="announcement-icon">
            <el-icon><Bell /></el-icon>
          </div>
          <div class="announcement-content">
            <div class="announcement-title">{{ announcement.title }}</div>
            <div class="announcement-summary">{{ announcement.summary }}</div>
            <div class="announcement-time">{{ formatDate(announcement.publishedAt) }}</div>
          </div>
          <div class="announcement-badge" v-if="announcement.isNew">
            <el-tag type="danger" size="small">新</el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  Document, User, EditPen, Medal, ArrowRight, Clock, Bell
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 响应式数据
const stats = ref({
  totalExams: '200+',
  totalStudents: '10万+',
  passRate: 85
})

const featuredExams = ref([
  {
    id: 1,
    title: 'JavaScript基础能力测试',
    description: '测试JavaScript基础语法、DOM操作、异步编程等核心知识点',
    category: '前端开发',
    difficulty: '初级',
    duration: 60,
    questionCount: 50,
    participants: 1250,
    passingScore: 60
  },
  {
    id: 2,
    title: 'Vue.js框架认证考试',
    description: 'Vue.js组件开发、状态管理、路由配置等高级特性考核',
    category: '前端框架',
    difficulty: '中级',
    duration: 90,
    questionCount: 40,
    participants: 890,
    passingScore: 70
  },
  {
    id: 3,
    title: 'Node.js后端开发测试',
    description: 'Express框架、数据库操作、API设计等后端开发技能评估',
    category: '后端开发',
    difficulty: '中级',
    duration: 120,
    questionCount: 60,
    participants: 650,
    passingScore: 65
  },
  {
    id: 4,
    title: 'React高级开发认证',
    description: 'React Hooks、性能优化、状态管理等高级开发技能认证',
    category: '前端框架',
    difficulty: '高级',
    duration: 150,
    questionCount: 45,
    participants: 420,
    passingScore: 75
  }
])

const recentRecords = ref([
  {
    id: 1,
    examTitle: 'JavaScript基础能力测试',
    score: 85,
    passed: true,
    completedAt: new Date('2024-01-15')
  },
  {
    id: 2,
    examTitle: 'CSS样式设计考试',
    score: 72,
    passed: true,
    completedAt: new Date('2024-01-10')
  },
  {
    id: 3,
    examTitle: 'HTML结构基础测试',
    score: 58,
    passed: false,
    completedAt: new Date('2024-01-05')
  }
])

const announcements = ref([
  {
    id: 1,
    title: '新增React高级开发认证考试',
    summary: '为满足前端开发者的进阶需求，现新增React高级开发认证考试...',
    publishedAt: new Date('2024-01-16'),
    isNew: true
  },
  {
    id: 2,
    title: '考试系统维护通知',
    summary: '系统将于本周末进行例行维护，维护期间暂停考试服务...',
    publishedAt: new Date('2024-01-14'),
    isNew: false
  },
  {
    id: 3,
    title: '证书申请流程优化',
    summary: '为提升用户体验，证书申请流程已优化，现支持在线申请...',
    publishedAt: new Date('2024-01-12'),
    isNew: false
  }
])

// 方法
const getDifficultyType = (difficulty) => {
  const types = {
    '初级': 'success',
    '中级': 'warning',
    '高级': 'danger'
  }
  return types[difficulty] || 'info'
}

const getScoreClass = (score) => {
  if (score >= 90) return 'excellent'
  if (score >= 80) return 'good'
  if (score >= 60) return 'pass'
  return 'fail'
}

const formatDate = (date) => {
  const now = new Date()
  const diff = now - date
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (days === 0) {
    return '今天'
  } else if (days === 1) {
    return '昨天'
  } else if (days < 7) {
    return `${days}天前`
  } else {
    return date.toLocaleDateString('zh-CN')
  }
}

const goToExamList = () => {
  router.push('/exam/list')
}

const goToMyExams = () => {
  router.push('/exam/my-exams')
}

const goToPractice = () => {
  router.push('/exam/practice')
}

const goToCertificates = () => {
  router.push('/exam/certificates')
}

const viewAllExams = () => {
  router.push('/exam/list')
}

const viewAllRecords = () => {
  router.push('/exam/records')
}

const viewAllAnnouncements = () => {
  router.push('/exam/announcements')
}

const viewExam = (exam) => {
  router.push(`/exam/detail/${exam.id}`)
}

const startExam = (exam) => {
  ElMessage.success(`准备开始考试：${exam.title}`)
  router.push(`/exam/take/${exam.id}`)
}

const viewResult = (record) => {
  router.push(`/exam/result/${record.id}`)
}

const retakeExam = (record) => {
  ElMessage.success(`重新开始考试：${record.examTitle}`)
}

const viewAnnouncement = (announcement) => {
  router.push(`/exam/announcement/${announcement.id}`)
}

onMounted(() => {
  // 初始化数据
})
</script>

<style lang="scss" scoped>
.exam-home {
  min-height: 100vh;
  background: var(--el-bg-color-page);
}

.welcome-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 0;
}

.banner-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  @media (max-width: 768px) {
    flex-direction: column;
    gap: 30px;
    text-align: center;
  }
}

.welcome-text {
  h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 16px;
  }
  
  p {
    font-size: 1.25rem;
    opacity: 0.9;
  }
}

.banner-stats {
  display: flex;
  gap: 40px;
  
  @media (max-width: 768px) {
    gap: 20px;
  }
}

.stat-item {
  text-align: center;
  
  .stat-number {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 4px;
  }
  
  .stat-label {
    font-size: 0.9rem;
    opacity: 0.8;
  }
}

.quick-actions {
  max-width: 1200px;
  margin: -30px auto 60px;
  padding: 0 20px;
  position: relative;
  z-index: 10;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.action-card {
  background: white;
  padding: 30px 24px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 20px;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  }
  
  .action-icon {
    width: 60px;
    height: 60px;
    background: var(--el-color-primary-light-9);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    
    .el-icon {
      font-size: 28px;
      color: var(--el-color-primary);
    }
  }
  
  .action-content {
    h3 {
      font-size: 18px;
      font-weight: 600;
      margin-bottom: 8px;
      color: var(--el-text-color-primary);
    }
    
    p {
      font-size: 14px;
      color: var(--el-text-color-secondary);
      margin: 0;
    }
  }
}

.featured-exams,
.recent-exams,
.exam-announcements {
  max-width: 1200px;
  margin: 0 auto 60px;
  padding: 0 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  
  h2 {
    font-size: 24px;
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
}

.exams-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.exam-card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
}

.exam-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.exam-category {
  font-size: 12px;
  color: var(--el-color-primary);
  background: var(--el-color-primary-light-9);
  padding: 4px 8px;
  border-radius: 4px;
}

.exam-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--el-text-color-primary);
}

.exam-desc {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 16px;
  line-height: 1.5;
}

.exam-meta {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--el-text-color-placeholder);
  
  .el-icon {
    color: var(--el-color-primary);
  }
}

.exam-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.exam-score {
  .label {
    font-size: 12px;
    color: var(--el-text-color-secondary);
  }
  
  .value {
    font-weight: 500;
    color: var(--el-text-color-primary);
  }
}

.records-list {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.record-item {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--el-border-color-extra-light);
  
  &:last-child {
    border-bottom: none;
  }
  
  .record-info {
    flex: 1;
    
    .exam-name {
      font-weight: 500;
      margin-bottom: 4px;
      color: var(--el-text-color-primary);
    }
    
    .exam-time {
      font-size: 12px;
      color: var(--el-text-color-placeholder);
    }
  }
  
  .record-score {
    margin-right: 20px;
    text-align: center;
    
    .score {
      font-size: 18px;
      font-weight: 700;
      margin-bottom: 4px;
      
      &.excellent {
        color: var(--el-color-success);
      }
      
      &.good {
        color: var(--el-color-primary);
      }
      
      &.pass {
        color: var(--el-color-warning);
      }
      
      &.fail {
        color: var(--el-color-danger);
      }
    }
  }
  
  .record-actions {
    display: flex;
    gap: 8px;
  }
}

.announcements-list {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.announcement-item {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--el-border-color-extra-light);
  cursor: pointer;
  transition: background 0.3s ease;
  
  &:hover {
    background: var(--el-fill-color-extra-light);
  }
  
  &:last-child {
    border-bottom: none;
  }
  
  .announcement-icon {
    width: 40px;
    height: 40px;
    background: var(--el-color-warning-light-9);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
    flex-shrink: 0;
    
    .el-icon {
      font-size: 20px;
      color: var(--el-color-warning);
    }
  }
  
  .announcement-content {
    flex: 1;
    
    .announcement-title {
      font-weight: 500;
      margin-bottom: 4px;
      color: var(--el-text-color-primary);
    }
    
    .announcement-summary {
      font-size: 14px;
      color: var(--el-text-color-secondary);
      margin-bottom: 4px;
      @include text-ellipsis(2);
    }
    
    .announcement-time {
      font-size: 12px;
      color: var(--el-text-color-placeholder);
    }
  }
  
  .announcement-badge {
    margin-left: 12px;
  }
}

@media (max-width: 768px) {
  .action-card {
    flex-direction: column;
    text-align: center;
    
    .action-content {
      text-align: center;
    }
  }
  
  .exams-grid {
    grid-template-columns: 1fr;
  }
  
  .record-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    
    .record-score {
      margin-right: 0;
      text-align: left;
      display: flex;
      align-items: center;
      gap: 12px;
    }
    
    .record-actions {
      width: 100%;
      justify-content: flex-end;
    }
  }
}
</style>
